/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.ecms.webSetting.service.impl;

import com.ecms.webSetting.domain.MessageSetting;
import com.ecms.webSetting.repository.WebRelateConfigRepository;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import com.ecms.webSetting.repository.MessageSettingRepository;
import com.ecms.webSetting.service.MessageSettingService;
import com.ecms.webSetting.service.dto.MessageSettingDto;
import com.ecms.webSetting.service.dto.MessageSettingQueryCriteria;
import com.ecms.webSetting.service.mapstruct.MessageSettingMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author ecms
* @date 2021-12-13
**/
@Service
@RequiredArgsConstructor
public class MessageSettingServiceImpl implements MessageSettingService {
    private final WebRelateConfigRepository webRelateConfigRepository;
    private final MessageSettingRepository messageSettingRepository;
    private final MessageSettingMapper messageSettingMapper;

    @Override
    public Map<String,Object> queryAll(MessageSettingQueryCriteria criteria, Pageable pageable){
        Page<MessageSetting> page = messageSettingRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(messageSettingMapper::toDto));
    }

    @Override
    public List<MessageSettingDto> queryAll(MessageSettingQueryCriteria criteria){
        return messageSettingMapper.toDto(messageSettingRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public MessageSettingDto findById(Long id) {
        MessageSetting messageSetting = messageSettingRepository.findById(id).orElseGet(MessageSetting::new);
        ValidationUtil.isNull(messageSetting.getId(),"MessageSetting","id",id);
        return messageSettingMapper.toDto(messageSetting);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MessageSettingDto create(MessageSetting resources) {
        resources.setCreateBy(SecurityUtils.getCurrentUsername());
        resources.setUpdateBy(SecurityUtils.getCurrentUsername());
        return messageSettingMapper.toDto(messageSettingRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MessageSetting resources) {
        resources.setUpdateBy(SecurityUtils.getCurrentUsername());
        MessageSetting messageSetting = messageSettingRepository.findById(resources.getId()).orElseGet(MessageSetting::new);
        ValidationUtil.isNull( messageSetting.getId(),"MessageSetting","id",resources.getId());
        messageSetting.copy(resources);
        messageSettingRepository.save(messageSetting);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            messageSettingRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<MessageSettingDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (MessageSettingDto messageSetting : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("标题", messageSetting.getTitle());
            map.put("内容", messageSetting.getContext());
            map.put("英文内容", messageSetting.getEnContext());
            map.put("外链", messageSetting.getHrefLink());
            map.put("图片路径", messageSetting.getImageUrl());
            map.put("创建者", messageSetting.getCreateBy());
            map.put("更新者", messageSetting.getUpdateBy());
            map.put("创建日期", messageSetting.getCreateTime());
            map.put("更新时间", messageSetting.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}